package cn.com.infcn.monitor.task.system;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.hyperic.sigar.FileSystem;
import org.hyperic.sigar.FileSystemUsage;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;

import cn.com.infcn.monitor.bean.IFCFileSystem;
import cn.com.infcn.monitor.task.BaseTask;
import cn.com.infcn.monitor.util.IFCBeanUtils;

/**
 * 获取文件系统信息
 * 
 * @author jjs
 *
 */
public class FileSystemTask implements BaseTask {

	private static final Logger LOGGER = Logger.getLogger(FileSystemTask.class);

	@Override
	public List<IFCFileSystem> execute() throws Exception {
		return fileSystem();
	}

	private List<IFCFileSystem> fileSystem() throws SigarException {

		try{
			Sigar sigar = new Sigar();
			FileSystem fslist[] = sigar.getFileSystemList();
			List<IFCFileSystem> list = new ArrayList<IFCFileSystem>();
			for (int i = 0; i < fslist.length; i++) {
	
				FileSystem fs = fslist[i];
				switch (fs.getType()) {
				case 0: // TYPE_UNKNOWN ：未知
					break;
				case 1: // TYPE_NONE
					break;
				case 2: // TYPE_LOCAL_DISK : 本地硬盘
					FileSystemUsage usage = sigar.getFileSystemUsage(fs
							.getDirName());
	
					IFCFileSystem fileSystem = new IFCFileSystem();
					IFCBeanUtils.copyProperties(fs, fileSystem);
					IFCBeanUtils.copyProperties(usage, fileSystem);
					list.add(fileSystem);
					break;
				case 3:// TYPE_NETWORK ：网络
					break;
				case 4:// TYPE_RAM_DISK ：闪存
					break;
				case 5:// TYPE_CDROM ：光驱
					break;
				case 6:// TYPE_SWAP ：页面交换
					break;
				}
			}
			return list;
		} catch (Exception e) {
			LOGGER.warn(e.getMessage(), e);
		}
		return null;
	}
	
	/*private void copyProperties(FileSystem fs, IFCFileSystem fileSystem){
		fileSystem.setDevName(fs.getDevName());
		fileSystem.setDirName(fs.getDirName());
		fileSystem.setFlags(fs.getFlags());
		fileSystem.setOptions(fs.getOptions());
		fileSystem.setSysTypeName(fs.getSysTypeName());
		fileSystem.setType(fs.getType());
		fileSystem.setTypeName(fs.getTypeName());
	}
	
	private void copyProperties(FileSystemUsage usage, IFCFileSystem fileSystem){
		fileSystem.setAvail(usage.getAvail());
		fileSystem.setDiskQueue(usage.getDiskQueue());
		fileSystem.setDiskReadBytes(usage.getDiskReadBytes());
		fileSystem.setDiskReads(usage.getDiskReads());
		fileSystem.setDiskServiceTime(usage.getDiskServiceTime());
		fileSystem.setDiskWriteBytes(usage.getDiskWriteBytes());
		fileSystem.setDiskWriteBytes(usage.getDiskWrites());
		fileSystem.setFiles(usage.getFiles());
		fileSystem.setFree(usage.getFree());
		fileSystem.setFreeFiles(usage.getFreeFiles());
		fileSystem.setTotal(usage.getTotal());
		fileSystem.setUsed(usage.getUsed());
		fileSystem.setUsePercent(usage.getUsePercent());
	}*/

}
